<?php
/*
 * Created on Oct 25, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("utils/FileUtils.php");							//--> Files
include_once("utils/ImageUtils.php");						//--> ImageUtils

$fileUtils	= new FileUtils();	//--> Files
$imageUtils	= new ImageUtils();	//--> Image Utils

$fileName	= $_GET['fileName'];
$height		= $_GET['height'];
$width		= $_GET['width'];
$type		= $_GET['type'];

if(empty($height))
	$height = 0;
	
if(empty($width))
	$width = 0;

$arr_extend_name 	= explode('.',$fileName);
$extend_name 		= $arr_extend_name[count($arr_extend_name) - 1];

$imageid = $arr_extend_name[count($arr_extend_name) - 2];

$homePath;
if($type === 'product'){
	$homePath = IMAGE_PATH_PRODUCT;
	
	$defaultImageFile 	= $homePath . 'noimage.jpg';
	$filepath = $homePath . $fileUtils->getDirById($imageid) . '/' . $imageid . '.' . $extend_name;
	
	if(!file_exists($filepath) || !is_file($filepath)){
		$filepath = $defaultImageFile;
	}
}else if ($type === 'site'){
	$homePath = IMAGE_PATH_SITE;
	
	$defaultImageFile 	= $homePath . 'noimage.jpg';
	$filepath 			= $homePath . $fileUtils->getDirById($imageid) . '/' . $imageid . '.' . $extend_name;
	
	if(!file_exists($filepath) || !is_file($filepath)){
		$filepath = $defaultImageFile;
	}
}else if ($type === 'logo'){
	$homePath = IMAGE_PATH_LOGO;
	
	$defaultImageFile 	= $homePath . 'merchant_noimage_logo.gif';
	$filepath 			= $homePath . $fileUtils->getDirById($imageid) . '/' . $imageid . '.' . $extend_name;	
	
	if(!file_exists($filepath) || !is_file($filepath)){
		$filepath = $defaultImageFile;
	}
}else if ($type === 'url'){
	$homePath = IMAGE_PATH_SITE;
	
	$defaultImageFile 	= $homePath . 'noimage.jpg';
	$filepath 			= base64_decode($fileName);
	
	$url = getFinalUrl($filepath);
	if(isset($url) && !empty($url))
		$filepath = $url;	
}else{
	$filepath 			= IMAGE_PATH_SITE . 'noimage.jpg';
	$defaultImageFile 	= IMAGE_PATH_SITE . 'noimage.jpg';
}



//echo 'filepath = ' . $filepath;

//$defaultImageFile = IMAGE_PATH_SITE . 'noimage.jpg';



//echo 'IF_NONE_MATCH = ' . $_SERVER['HTTP_IF_NONE_MATCH'];		
//echo 'path = ' . file_exists($filepath) . '<br>';
//echo 'path = ' . $filepath;
//echo 'width = ' . $width;
//echo 'height = ' . $height;
//exit;

$imageUtils->RatioAdjuct($filepath, $width, $height, false, true, $defaultImageFile);

function getFinalUrl($durl){
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $durl);
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	$content = curl_exec($ch);
	
	curl_close($ch);
	
	preg_match('/Location:(.*?)\n/', $content, $matches); 
	
//	echo $content;
	
//	var_dump($matches);

	return $matches[1];
	
}

function curl_file_get_contents($durl){
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $durl);
  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
  curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);  
  
  $r = curl_exec($ch);
  curl_close($ch);
   return $r;
}

?>
